﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using CH.Easy.DbProvider.Common;
using CH.Easy.DbProvider.Mapping;
namespace CH.Easy.DbProvider.ExpressionExtend
{
    public class AddExpression : Expression
    {

        public Dictionary<string, object> ChangeValue { get; set; }
        public string Alias { get; set; }
        public string Name { get; set; }
        public string PrimaryKey { get; set; }
        public bool IsIdentity { get; set; }
        public Type t { get; set; }
        public AddExpression(object obj)
        {


            t = obj.GetType();
            this.Alias = "T" + t.GetHashCode();
            this.Name = t.Name;
            this.PrimaryKey = "ID";
            this.IsIdentity = true;
            var attributes = t.GetCustomAttributes(typeof(TableMappingAttribute), true);
            if (attributes != null && attributes.Count() > 0)
            {
                foreach (var attribute in attributes)
                {
                    TableMappingAttribute a = attribute as TableMappingAttribute;
                    if (a != null)
                    {
                       
                        this.Name = a.Name;
                        this.PrimaryKey = a.PrimaryKey;
                        this.IsIdentity = a.IsIdentity;
                    }
                }
            }
            this.ChangeValue = new Dictionary<string, object>();
            foreach (var p in t.GetProperties())
            {
                 //是主键  是引用类型但是不是字符串类型
                if ((p.Name == this.PrimaryKey && this.IsIdentity)||(!p.PropertyType.IsValueType && p.PropertyType != typeof(string)))
                    continue;



                ChangeValue.Add(p.Name, t.GetProperty(p.Name).GetValue(obj, null));
            }

        }
        public override ExpressionType NodeType
        {
            get
            {
                return (ExpressionType)ExpressionTypeExend.Insert;
            }
        }

        public override Type Type
        {
            get
            {

                return typeof(int);
            }
        }

    }
}
